package entity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 书架类
 */
public class BookList {
    /**
     * 书架中的所有图书
     */
    public static List<Book> books = new ArrayList<>();
    /**
     * 书架中所有书的名字，方便于查找该书是否已存在
     */
    public static List<String> bookNames = new ArrayList<>();
    public static List<Book> bookBorrowed = new ArrayList<>();

    /**
     * 默认添加四大名著
     */
    static {
        books.add(new Book("红楼梦", "曹雪芹", 88.8, "长篇小说"));
        books.add(new Book("水浒传", "施耐庵", 88.8, "长篇小说"));
        books.add(new Book("三国演义", "罗贯中", 88.8, "长篇小说"));
        books.add(new Book("西游记", "吴承恩", 88.8, "长篇小说"));
        String[] allBookName = new String[]{"红楼梦", "水浒传", "三国演义", "西游记"};
        bookNames.addAll(Arrays.asList(allBookName));
    }

    /**
     * 添加书本
     * @param book
     */
    public void add(Book book) {
        books.add(book);
    }

    /**
     * 展示当前书架中的所有书
     */
    public void displayBooks(){
        for (Book book : books) {
            System.out.println(book);
        }
    }

    public Book findBook(String bookName) {
        for (Book book : books) {
            if (book.getBookName().equals(bookName)) {
                return book;
            }
        }
        return null;
    }

    /**
     * 判断该书是否已存在于该书架中
     * @param bookName
     * @return
     */
    public boolean contain(String bookName) {
        return bookNames.contains(bookName);
    }

    /**
     * 删除图书
     * @param bookName
     */
    public boolean deleteBook(String bookName) {
        for (Book bookTemp : books) {
            if (bookTemp.getBookName().equals(bookName)) {
                books.remove(bookTemp);
                bookNames.remove(bookName);
                return true;
            }
        }
        return false;
    }

    /**
     * 存储已经借出的图书
     * @return
     */
    public void borrowed(String bookName){
        for (Book book : books) {
            if (book.getBookName().equals(bookName)) {
                bookBorrowed.add(book);
                books.remove(book);
                bookNames.remove(bookName);
                return;
            }
        }
    }
}
